from SdlDotNet import *
from System.Drawing import Color, Point
from kant import KC
from page import Page

class CodeFilePage(Page):
    highlight = None
    def __init__(self, title, file, highlight='', skiphashbang=False, **kw):
        self.pagetext = [line.rstrip() for line in open(file)]
        self.title = title
        lines = [line.rstrip() for line in open(file)]

        # handle hidden lines       (end comment #--)
        m = []
        skip = False
        for line in lines:
            if skiphashbang and line.startswith("#!"): continue
            if line.endswith('#--'):
                if not skip:
                    m.append('...')
                    skip = True
                else:
                    continue
            else:
                m.append(line)
                skip = False
        lines = m

        if highlight:
            # handle title
            print "highlight", self.highlight
            self.pagetext = []
            for l in lines:
                if not l.startswith('#='):
                    self.pagetext.append(l)
                if l.startswith('#='+highlight):
                    els = l.split(' ', 1)
                    if len(els) == 2:
                        self.title = l[len(highlight)+2:].strip()
        else:
            print "no highlight"
            self.pagetext = [line.rstrip() for line in open(file)]
        self.fgcolour = KC.codecolour
        self.block_bgcolour = KC.code_bgcolour

        self.highlight = highlight
        Page.__init__(self, **kw)

    def init(self):
        screen = Video.Screen
        sw = screen.Width
        fontsize = 64
        if self.title:
            font = KC.titlefont
            self.title = font.Render(self.title, KC.titlecolour)
            if self.title.Width > sw:
                self.title.Scale(float(sw)/self.title.Width, True)
            tH = self.title.Height
        else:
            tH = 0

        font = KC.codefont
        lines = []
        for line in self.pagetext:
            HL = len(self.highlight)
            colour = self.fgcolour
            if self.highlight:
                if line.startswith('#='): continue
                if line.endswith('#'+self.highlight):
                    colour = KC.code_hilitecolour
                if line.endswith('//'+self.highlight):
                    colour = KC.code_hilitecolour
                if len(line) > HL and line[-(HL+1)] == '#':
                    line = line[:-(HL+1)].rstrip()
                if len(line) > HL and line[-(HL+2):-(HL)] == '//':
                    line = line[:-(HL+2)].rstrip()
            line = font.Render(line, colour)
            lines.append(line)

        w = max([x.Width for x in lines])
        h = sum([x.Height for x in lines])
        self.pageblock = Surface(w + 20, h + 20)
        self.pageblock.Fill(self.block_bgcolour)
        y = 0
        for line in lines:
            self.pageblock.Blit(line, Point(10, 10+y))
            y += line.Height
        # scale pageblock
        sw, sh = screen.Width, screen.Height - tH
        pw, ph = self.pageblock.Width, self.pageblock.Height
        scale = min(float(sh)/ph, float(sw)/pw)
        scale = scale * 0.85
        if scale < 1:
            self.pageblock.Scale(scale, True)
    
        Page.init(self)

    def render(self):
        screen = Video.Screen
        sw, sh = screen.Width, screen.Height

        tw, th = self.title.Width, self.title.Height
        screen.Blit(self.title, Point(sw/2 - tw/2, 0))

        bw, bh = self.pageblock.Width, self.pageblock.Height
        l = sw/2 - bw/2
        t = th + (sh-th)/2 - bh/2
        screen.Blit(self.pageblock, Point(l, t))
        Page.render(self)


